<?php
namespace App\Http\Controllers\Wxxcx;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    protected $service;

    public function __construct()
    {
    }

    /**
     * 返回接口
     */
    public function toJson($code = 0, $msg = '', $data = [], $total = 0)
    {
        $resData = [
            'code' => $code,
            'msg' => $msg ? $msg : 'success',
            'data' => $data,
            'total' => $total,
        ];
        if (!empty($data)) $resData['data'] = $data;
        return $resData;
    }

    /**
     * 自定义验证
     * @param array $pas【$request->all()】 请求参数
     * @param array $fields 字段信息【en字段名，cn字段中文名，ismust是否必须】
     */
    public function checkField($pas, $fields)
    {
        $fkeys = [];
        $fieldArys = [];
        foreach ($fields as $k => $v) {
            $fieldArys[$v['en']] = isset($pas[$k]) ? $pas[$k] : '';
            if ((!isset($pas[$k]) || !$pas[$k])
                && (isset($v['ismust']) && $v['ismust'])) {
                //判断字段的值有没有，并且看看字段是否必须的
                $fkeys[] = $fields[$k]['cn'] . '必须的';
            }
        }
        if (count($fkeys) > 0) {
            return array('code' => -2, 'msg' => $fkeys[0], 'data' => $fieldArys);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $fieldArys);
    }
}